var util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: "100%",
    positionInfo:{},
    nonet: true ,
    isExit:0,
    hasSendEmailText:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var positionId = options.pid;
    var that = this;
     //获取职位详情 
    util.post(app.api.getSchJobById+"/"+positionId,null,function(res){
    that.setData({
      positionInfo: res.data
    });
    })
    //获取是否可投递
    util.post(app.api.hasSendEmail,{
      "unionid":app.globalData.unionid,
      "jobId":positionId
    },function(res){
      if(res.data) {
        that.setData({
          hasSendEmailText: '已投递'
        })
      }
    })
  },
  //公司详情
  // companyDetailTap: function (event) {
  //    var id = event.currentTarget.dataset.pid; // 当前id
  //    wx.navigateTo({
  //      url: '../company-detail/company-detail?id=' + id
  //    });
  //  },
  //发送简历
  isSendTap: function (e) {
    var that = this;
    wx.showModal({
      title: '申请确认',
      content: '您确定要申请该职位吗？',
      cancelText: '取消',
      cancelColor: '#999',
      confirmText: '确认',
      confirmColor: '#4990E2',
      success: function (res) {
        if (res.confirm) {
          that.sendResumeFun();
        } 
      }
    })
  },
  //投递接口
  sendResumeFun: function () {
    var that = this;
    util.post(app.api.hasSendEmail,{
      "unionid":app.globalData.unionid,
      "jobId":that.data.positionInfo.id
    },function(res){
      console.log(res.data)
      if(res.data) {
        app.alert("您已投递该职位，不能重复投递！");
      } else {
        util.post(app.api.sendEmail, {
          jobId: that.data.positionInfo.id,
          unionid: app.globalData.unionid
        }, function (data) {
          wx.showModal({
            title: '投递成功',
            icon: 'success',
            duration: 2000
          })
         wx.redirectTo({
           url: '/pages/schJobDetail/index?pid='+that.data.positionInfo.id,
         })
        });
      }
    })
  },

  //分享
  onShareAppMessage: function (res) {
    var that = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    var title = '职位详情'
    return {
      title: title,
      path: 'pages/schJobDetail/index?pid=' + that.data.positionInfo.id,
      data: that.data.positionInfo,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})